// In a network of nodes, each node i is directly connected to another node j if and only if graph[i][j] = 1.

// Some nodes initial are initially infected by malware.  Whenever two nodes are directly connected and 
// at least one of those two nodes is infected by malware, both nodes will be infected by malware.  
// This spread of malware will continue until no more nodes can be infected in this manner.

// Suppose M(initial) is the final number of nodes infected with malware in the entire network, after the 
// spread of malware stops.

// We will remove one node from the initial list.  Return the node that if removed, would minimize M(initial). 
//  If multiple nodes could be removed to minimize M(initial), return such a node with the smallest index.

// Note that if a node was removed from the initial list of infected nodes, it may still be infected later as 
// a result of the malware spread.

 
// Example 1:

// Input: graph = [[1,1,0],[1,1,0],[0,0,1]], initial = [0,1]
// Output: 0
// Example 2:

// Input: graph = [[1,0,0],[0,1,0],[0,0,1]], initial = [0,2]
// Output: 0
// Example 3:

// Input: graph = [[1,1,1],[1,1,1],[1,1,1]], initial = [1,2]
// Output: 1


First, let's color (the nodes of) each component of the graph. We can do this using a depth first search.                          

Afterwards, notice that if two nodes in initial have the same color (ie., belong to the same component), 
then removing them from initial won't decrease M(initial). This is because the malware will spread                                  
to reach every node in this component no matter what.

So, among nodes with a unique color in initial, we will remove the node with the largest component size. 
(If there's a tie, we return the smallest index. Also, if there aren't any nodes with a unique color, 
we'll just return the smallest index node.)                                                                                     

SOLUTION, DFS SEARCH AND COLOR 
Algorithm

This algorithm has a few parts:

1. Coloring each component: For each node, if it isn't yet colored, use.                                                       
a depth-first search to traverse its component, coloring that component with a new color.

2. Size of each color: Count the number of occurrences of each color.

3. Find unique colors: Look at the colors of nodes in initial to see which nodes have unique colors.

4. Choose answer: For each node with a unique color, find the size of that color. 
The largest size is selected, with ties broken by lowest node number.

If there is no node with a unique color, the answer is min(initial).


TC: O(N^2) where N is the length of the graph 
SC: O(N) for the colors array 

class Solution {
    public int minMalwareSpread(int[][] graph, int[] initial) {
        // 1. Color each component.
        // colors[node] = the color of this node.

        int N = graph.length;
        int[] colors = new int[N];
        Arrays.fill(colors, -1);
        int C = 0;

        for (int node = 0; node < N; ++node)
            if (colors[node] == -1)
                dfs(graph, colors, node, C++);

        // 2. Size of each color.
        int[] size = new int[C];
        for (int color: colors)
            size[color]++;

        // 3. Find unique colors.
        int[] colorCount = new int[C];
        for (int node: initial)
            colorCount[colors[node]]++;

        // 4. Answer
        int ans = Integer.MAX_VALUE;
        for (int node: initial) {
            int c = colors[node];
            if (colorCount[c] == 1) {
                if (ans == Integer.MAX_VALUE)
                    ans = node;
                else if (size[c] > size[colors[ans]])
                    ans = node;
                else if (size[c] == size[colors[ans]] && node < ans)
                    ans = node;
            }
        }

        if (ans == Integer.MAX_VALUE)
            for (int node: initial)
                ans = Math.min(ans, node);

        return ans;
    }

    public void dfs(int[][] graph, int[] colors, int node, int color) {
        colors[node] = color;
        for (int nei = 0; nei < graph.length; ++nei)
            if (graph[node][nei] == 1 && colors[nei] == -1)
                dfs(graph, colors, nei, color);
    }
}